﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class NhanVienLuongBO : Csla.BusinessBase<NhanVienLuongBO>
    {
        #region Business Properties

        private int? _NhanVienID = null;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; }
        }

        private string _MaNhanVien = null;
        public string MaNhanVien
        {
            get { return this._MaNhanVien; }
            set { this._MaNhanVien = value; }
        }

        private string _Ho = null;
        public string Ho
        {
            get { return this._Ho; }
            set { this._Ho = value; }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; }
        }

        private bool? _Nu = false;
        public bool? Nu
        {
            get { return this._Nu; }
            set { this._Nu = value; }
        }

        private int? _PhongBanID = null;
        public int? PhongBanID
        {
            get { return this._PhongBanID; }
            set { this._PhongBanID = value; }
        }

        private int? _ChucVuID = null;
        public int? ChucVuID
        {
            get { return this._ChucVuID; }
            set { this._ChucVuID = value; }
        }

        private int? _TinhTrangCongViecID = 0;
        public int? TinhTrangCongViecID
        {
            get { return this._TinhTrangCongViecID; }
            set { this._TinhTrangCongViecID = value; }
        }

        private DateTime? _NgayKetThucLamViec = null;
        public DateTime? NgayKetThucLamViec
        {
            get { return this._NgayKetThucLamViec; }
            set { this._NgayKetThucLamViec = value; }
        }

        private double? _LuongBHXH = 0;
        public double? LuongBHXH
        {
            get { return this._LuongBHXH; }
            set { this._LuongBHXH = value; }
        }

        private bool? _BHXH = false;
        public bool? BHXH
        {
            get { return this._BHXH; }
            set { this._BHXH = value; }
        }

        private bool? _BHYT = false;
        public bool? BHYT
        {
            get { return this._BHYT; }
            set { this._BHYT = value; }
        }

        private bool? _BHTN = false;
        public bool? BHTN
        {
            get { return this._BHTN; }
            set { this._BHTN = value; }
        }

        private double? _BHXH_PCCV = 0;
        public double? BHXH_PCCV
        {
            get { return this._BHXH_PCCV; }
            set { this._BHXH_PCCV = value; }
        }

        private double? _BHXH_TNN = 0;
        public double? BHXH_TNN
        {
            get { return this._BHXH_TNN; }
            set { this._BHXH_TNN = value; }
        }

        private double? _BHXH_TNVK = 0;
        public double? BHXH_TNVK
        {
            get { return this._BHXH_TNVK; }
            set { this._BHXH_TNVK = value; }
        }

        private double? _BHXH_TNK = 0;
        public double? BHXH_TNK
        {
            get { return this._BHXH_TNK; }
            set { this._BHXH_TNK = value; }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _NhanVienID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newNhanVienID = -1;
        private NhanVienLuongBO()
        {
            _NhanVienID = newNhanVienID--;
        }

        public NhanVienLuongBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public NhanVienLuongBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static NhanVienLuongBO NewRootNhanVienLuongBO()
        {
            return DataPortal.Create<NhanVienLuongBO>(new RootCriteria());
        }

        internal static NhanVienLuongBO NewChildNhanVienLuongBO()
        {
            return DataPortal.Create<NhanVienLuongBO>(new ChildCriteria());
        }

        public static NhanVienLuongBO GetRootNhanVienLuongBO(int _NhanVienID)
        {
            return DataPortal.Fetch<NhanVienLuongBO>(new RootCriteria(_NhanVienID));
        }

        public static NhanVienLuongBO GetRootNhanVienLuongBOByWhere(string where)
        {
            return DataPortal.Fetch<NhanVienLuongBO>(where);
        }

        internal static NhanVienLuongBO GetChildNhanVienLuongBO(SqlDataReader dataReader)
        {
            return new NhanVienLuongBO(dataReader);
        }

        public static void DeleteNhanVienLuongBO(int _NhanVienID)
        {
            DataPortal.Delete(new RootCriteria(_NhanVienID));
        }

        public static void DeleteNhanVienLuongBO(int _NhanVienID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_NhanVienID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int NhanVienID;
            public bool directDelete = false;
            public RootCriteria(int _NhanVienID)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = false;
            }

            public RootCriteria(int _NhanVienID, bool directDelete)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int NhanVienID;
            public bool directDelete = false;
            public ChildCriteria(int _NhanVienID)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = false;
            }

            public ChildCriteria(int _NhanVienID, bool directDelete)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienLuongBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[NhanVienID] = " + criteria.NhanVienID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhanVienLuongBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["NhanVienID"] is DBNull))
                this._NhanVienID = (int)dataReader["NhanVienID"];
            if (!(dataReader["MaNhanVien"] is DBNull))
                this._MaNhanVien = (string)dataReader["MaNhanVien"];
            if (!(dataReader["Ho"] is DBNull))
                this._Ho = (string)dataReader["Ho"];
            if (!(dataReader["Ten"] is DBNull))
                this._Ten = (string)dataReader["Ten"];
            if (!(dataReader["Nu"] is DBNull))
                this._Nu = (bool)dataReader["Nu"];
            if (!(dataReader["PhongBanID"] is DBNull))
                this._PhongBanID = (int)dataReader["PhongBanID"];
            if (!(dataReader["ChucVuID"] is DBNull))
                this._ChucVuID = (int)dataReader["ChucVuID"];
            if (!(dataReader["TinhTrangCongViecID"] is DBNull))
                this._TinhTrangCongViecID = (int)dataReader["TinhTrangCongViecID"];
            if (!(dataReader["NgayKetThucLamViec"] is DBNull))
                this._NgayKetThucLamViec = (DateTime)dataReader["NgayKetThucLamViec"];
            if (!(dataReader["LuongBHXH"] is DBNull))
                this._LuongBHXH = (double)dataReader["LuongBHXH"];
            if (!(dataReader["BHXH"] is DBNull))
                this._BHXH = (bool)dataReader["BHXH"];
            if (!(dataReader["BHYT"] is DBNull))
                this._BHYT = (bool)dataReader["BHYT"];
            if (!(dataReader["BHTN"] is DBNull))
                this._BHTN = (bool)dataReader["BHTN"];
            if (!(dataReader["BHXH_PCCV"] is DBNull))
                this._BHXH_PCCV = (double)dataReader["BHXH_PCCV"];
            if (!(dataReader["BHXH_TNN"] is DBNull))
                this._BHXH_TNN = (double)dataReader["BHXH_TNN"];
            if (!(dataReader["BHXH_TNVK"] is DBNull))
                this._BHXH_TNVK = (double)dataReader["BHXH_TNVK"];
            if (!(dataReader["BHXH_TNK"] is DBNull))
                this._BHXH_TNK = (double)dataReader["BHXH_TNK"];
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_NhanVienID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_NhanVienID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
